package com.naire.modul.questionnaire.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.util.BeanUtils;

import com.naire.modul.project.pojo.Project;
import com.naire.modul.questionnaire.dao.Projeckpi_answerDao;
import com.naire.modul.questionnaire.pojo.Projeckpi_answer;
import com.naire.modul.questionnaire.pojo.Projecoption;
import com.naire.modul.questionnaire.vo.Projeckpi_answerVo;
import com.naire.modul.questionnaire.vo.ProjecoptionVo;

public class Projeckpi_answerDaoImpl extends GeneralDaoImpl implements
		Projeckpi_answerDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Projeckpi_answerVo> search(Integer project_id, String kpi_num) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("project_id", project_id);
		params.put("kpi_num", kpi_num);
		StringBuilder hql = new StringBuilder("from Projeckpi_answer where kpi_num=:kpi_num and project.project_id=:project_id");
		List<Projeckpi_answerVo> d = new ArrayList<Projeckpi_answerVo>();
		List<Projeckpi_answer> l = hibernateHandler.query(hql, params);
		for (Projeckpi_answer bean : l) {
			Projeckpi_answerVo dv = new Projeckpi_answerVo();
			BeanUtils.copyProperties(bean, dv);
			Iterator<Projecoption> iterator = bean.getProjecoptions().iterator();
			while(iterator.hasNext()) {
				Projecoption p = iterator.next();
				ProjecoptionVo pv = new ProjecoptionVo();
				BeanUtils.copyProperties(p, pv);
				dv.getOptions().add(pv);
			}
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(Projeckpi_answerVo bean) throws Exception {
		Project project = (Project) hibernateHandler.find(Project.class,
				bean.getProject_id());
		Projeckpi_answer pojo = new Projeckpi_answer();
		BeanUtils.copyProperties(bean, pojo);
		pojo.setProject(project);
		int i = 0;
		for(ProjecoptionVo pv : bean.getOptions()) {
			if(pv != null) {
				Projecoption p = new Projecoption();
				BeanUtils.copyProperties(pv, p);
				p.setSeq_num(i);
				p.setProjeckpi_answer(pojo);
				pojo.getProjecoptions().add(p);
				i++;
			}
		}
		hibernateHandler.insert(pojo);
	}

	@Override
	public void update(Projeckpi_answerVo bean) throws Exception {
		Projeckpi_answer pojo = (Projeckpi_answer) hibernateHandler.find(
				Projeckpi_answer.class, bean.getProject_answer_id());
		BeanUtils.copyProperties(bean, pojo);
		pojo.getProjecoptions().clear();
		int i = 0;
		for(ProjecoptionVo pv : bean.getOptions()) {
			if(pv != null) {
				Projecoption p = new Projecoption();
				BeanUtils.copyProperties(pv, p);
				p.setSeq_num(i);
				p.setProjeckpi_answer(pojo);
				pojo.getProjecoptions().add(p);
				i++;
			}
		}
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(Projeckpi_answerVo bean) throws Exception {
		update(bean);
	}

	@Override
	public Projeckpi_answerVo find(Integer id) throws Exception {
		Projeckpi_answer pojo = (Projeckpi_answer) hibernateHandler.find(
				Projeckpi_answer.class, id);
		Projeckpi_answerVo bean = new Projeckpi_answerVo();
		BeanUtils.copyProperties(pojo, bean);
		Iterator<Projecoption> iterator = pojo.getProjecoptions().iterator();
		while(iterator.hasNext()) {
			Projecoption p = iterator.next();
			ProjecoptionVo pv = new ProjecoptionVo();
			BeanUtils.copyProperties(p, pv);
			bean.getOptions().add(pv);
		}
		return bean;
	}

	@Override
	public void delete(Integer id) throws Exception {
		Projeckpi_answer pojo = (Projeckpi_answer) hibernateHandler.find(
				Projeckpi_answer.class, id);
		hibernateHandler.delete(pojo);
		
	}
}
